package com.wdy.action;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.excelutils.ExcelUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.wdy.po.Charge;
import com.wdy.service.ChargeManager;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;

@SuppressWarnings( { "serial", "unused", "unchecked" })
public class ChargeAction extends BasicAction {
	private static final Log log = LogFactory.getLog(ChargeAction.class);
	private Charge charge;
	private ChargeManager chargeManager;
	private Page page;
	private List<Condition> conList;
	private String[] ids;
	private Map<String, Object> session;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private ServletContext servletContext;

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<Condition> getConList() {
		return conList;
	}

	public void setConList(List<Condition> conList) {
		this.conList = conList;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public Charge getCharge() {
		return charge;
	}

	public void setCharge(Charge charge) {
		this.charge = charge;
	}

	public void setChargeManager(ChargeManager chargeManager) {
		this.chargeManager = chargeManager;
	}

	// charge list.action
	@SkipValidation
	public String listAll() {
		List<Charge> list = null;
		list = (List<Charge>) chargeManager.listAll();
		if (null != list) {
			request.setAttribute("chargeList", list);
		}
		return SUCCESS;
	}

	// charge add.action
	public String add() {
		chargeManager.saveCharge(charge);
		return SUCCESS;
	}

	// charge delete.action
	@SkipValidation
	public String delete() {
		chargeManager.removeCharge(charge);
		return SUCCESS;
	}

	// charge update.action
	public String update() {
		chargeManager.updateCharge(charge);
		return SUCCESS;
	}

	// charge findByExample.action
	@SkipValidation
	public String find() {
		List<Charge> list = null;
		list = (List<Charge>) chargeManager.findByExample(charge);
		if (null != list) {
			request.setAttribute("charges", list);
		}
		return SUCCESS;
	}

	// charge get.action
	@SkipValidation
	public String get() {
		Charge charge = null;
		charge = (Charge) chargeManager.getCharge(this.charge.getCid());
		if (null != charge) {
			request.setAttribute("charge", charge);
		}
		return SUCCESS;
	}

	// charge query_charge.action
	@SkipValidation
	public String query() throws Exception {
		// conList = (List<Condition>)session.get("conList");
		log.info("query action");
		if (null == conList || conList.size() == 0) {
			conList = new LinkedList<Condition>();
		}
		Iterator<Condition> it = conList.iterator();
		while (it.hasNext()) {
			Condition con = (Condition) it.next();
			if (null != con.getValues()) {
				Object[] values = con.getValues();
				if (null == values[0] || "".equals(values[0])) {
					it.remove();
				}
			} else {
				it.remove();
			}
		}

		if (null == page) {
			page = new Page();
			page.setEveryPage(5);
			page.setCurrentPage(1);
		}
		Result result = chargeManager.query(conList, page);
		request.setAttribute("page", result.getPage());
		request.setAttribute("chargeList", result.getContent());
		// session.put("conList", conList);
		return SUCCESS;
	}

	// charge delete_batch.action
	@SkipValidation
	public String deleteBatch() throws Exception {
		if (null != ids) {
			Integer[] id = new Integer[ids.length];
			for (int i = 0; i < ids.length; i++) {
				id[i] = new Integer(ids[i]);
			}
			chargeManager.removeCharges(id);
		}
		return SUCCESS;
	}

	// charge export.action
	public String exportExcel() throws Exception {
		log.info("export Excel action");
		if (null == conList || conList.size() == 0) {
			conList = new LinkedList<Condition>();
		}
		Iterator<Condition> it = conList.iterator();
		while (it.hasNext()) {
			Condition con = (Condition) it.next();
			if (null != con.getValues()) {
				Object[] values = con.getValues();
				if (null == values[0] || "".equals(values[0])) {
					it.remove();
				}
			} else {
				it.remove();
			}
		}

		List<Charge> chargeList = (List<Charge>) chargeManager.query(conList);
		ExcelUtils.addValue("printDate", getCurrentDate("yyyy-MM-dd"));
		ExcelUtils.addValue("list", chargeList);

		response.reset();
		String config = "/excel/charge_template.xls";
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ System.currentTimeMillis() + ".xls\"");
		OutputStream out = response.getOutputStream();
		ExcelUtils.export(servletContext, config, out);
		out.close();

		return SUCCESS;
	}

	protected String getCurrentDate(String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(new Date());
	}
}
